//
//  HDEasterEggsViewController.m
//  TestEasterEggs
//
//  Created by Mirko on 15/8/10.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HDEasterEggsViewController.h"
#import "HDEasterEggsInterface.h"
#import "HDEasterEggsRegistrationCenter.h"
#import <objc/runtime.h>
#import "HDEasterEggsCenter.h"
#import "HDEasterEggsTableViewCell.h"
#import "HDEasterEggsCenterModelDefine.h"
#import "UserDefaultsUtils.h"
#import "HDEasterEggsCellFactory.h"

const static CGFloat kLoadingBgViewWidth = 100;
const static CGFloat kLoadingBgViewHeight = kLoadingBgViewWidth;

const static CGFloat kLoadingViewWidth = 60;
const static CGFloat kLoadingViewHeight = kLoadingViewWidth;

const static CGFloat kCellRowHeight = 60;

static NSString *kEasterEggsTitle = @"Easter Eggs";

@interface HDEasterEggsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mTableView;
//@property (nonatomic, strong) NSArray *easterEggsList;
@property (nonatomic, strong) UIActivityIndicatorView *loadingView;
@property (nonnull, strong) HDEasterEggsCellFactory *factory;

@end


@implementation HDEasterEggsViewController

-(void) viewDidLoad
{
    self.navigationItem.title = kEasterEggsTitle;
    
    UIBarButtonItem *rightNavBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(handleNavRightButtonEvent:)];
    self.navigationItem.rightBarButtonItem = rightNavBtnItem;
    
    self.mTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.mTableView];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    
    [self installLoadingView];
    [self installTableViewCell];
    self.factory = [[HDEasterEggsCellFactory alloc] initWithTable:self.mTableView];
}

- (void)dealloc
{
    if (self.loadingView.isAnimating) {
        [self.loadingView stopAnimating];
    }
    [self.loadingView removeObserver:self forKeyPath:@"hidden" context:nil];
}

- (void)installLoadingView
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake((screenSize.width - kLoadingBgViewWidth) / 2, (screenSize.height - kLoadingBgViewHeight) / 2, kLoadingBgViewWidth, kLoadingBgViewHeight)];
    view.layer.cornerRadius = 8;
    view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    view.hidden = YES;
    self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.loadingView.frame = CGRectMake((view.frame.size.width - kLoadingViewWidth) / 2, (view.frame.size.height - kLoadingViewHeight) / 2, kLoadingViewWidth, kLoadingViewHeight);
    self.loadingView.hidesWhenStopped = YES;
    self.loadingView.hidden = YES;
    
    [self.loadingView addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    [view addSubview:self.loadingView];
    [self.view addSubview:view];
}

- (void)installTableViewCell {
    
//    UINib *nib = [UINib nibWithNibName:@"HDEasterEggsTableViewCell" bundle:[MCUtils MCPMBundle]];
//    [self.mTableView registerNib:nib forCellReuseIdentifier:@"HDEasterEggsTableViewCell"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"hidden"])
    {
        BOOL changed = [[change valueForKey:@"new"] boolValue];
        [self.loadingView superview].hidden = changed;
        self.mTableView.userInteractionEnabled = changed;
    }
}

- (void)handleNavRightButtonEvent:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"View controller dismiss completion!");
    }];
}

#pragma mark UITableView data source & delegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kCellRowHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.easterEggsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger idx = indexPath.row;
    id<HDEasterEggsDataSource> object = self.easterEggsList[idx];
    UITableViewCell *cell = [self.factory getTableCellWithDataSource:object index:(int)indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger idx = indexPath.row;
    id<HDEasterEggsDelegate> object = self.easterEggsList[idx];
    if ([object respondsToSelector:@selector(handleCellAction)]) {
        [object handleCellAction];
    }
    
    HDEETableCellModel *model = [self.factory getModelWithIdx:(int)indexPath.row];
    if (model.type == kHDEasterEggsCellTypeForJumpToVC) {
        [self.navigationController pushViewController:self.easterEggsList[idx] animated:YES];
    }
}

@end
